define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('F0104Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater) {
            /**
             * options : {
             *      material_item: 物料資訊
             *      wo_no: 工單編號
             *      op_no: 作業站編號
             *      callback : function
             * }
            **/
            this.init = function (options) {
                console.log('material_detail',options.material_item)
                if(options){
                    options.wo_no =  options.wo_no || '';
                    options.op_no = options.op_no || '';
                    options.material_item = options.material_item || {};
                    
                }else{
                    options = {wo_no: '', op_no: '', material_item: {}};
                }                
                //部份下料
                function portionCutting(dialog,callback){
                    var content = {
                        wo_no: dialog.wo_no,
                        op_no: dialog.op_no,
                        machine_no: dialog.material_detail.machine_no,
                        portion_cutting_qty: dialog.portion_cutting_qty >= 1 ? dialog.portion_cutting_qty : "",
                        material_detail: [dialog.material_detail],
                    }
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_MAT.cutting_process',
                        content : content,
                        success : function(data, response) {
                            $rootScope.showToast(response.Message);
                            if(callback)
                                callback();
                        }
                    });
                }
                //檢查
                function beforePortionCutting(dialog,callback){
                    if (dialog.portion_cutting_qty > dialog.material_detail.qty) {
                        $rootScope.showAlert($filter('translate')('F0104.msg.Exceeded_the_maximum_available_quantity'));
                    }else{
                        portionCutting(dialog,callback);
                    }
                }
                //小算盤
                function inputQty(target, dialog){
                    angular.element(target).addClass('is-focus');
                    $calculater.open(function(num, calcDialog){
                        angular.element(target).removeClass('is-focus');
                        if(+num <= 0){
                            $rootScope.showAlert($filter('translate')('F0104.msg.Must_be_greater_than_zero'));
                        }else{                            
                            calcDialog.hide();
                            dialog.portion_cutting_qty = (+num);
                        }
                    },
                    function(calcDialog){
                        angular.element(target).removeClass('is-focus');
                        calcDialog.hide();
                    },
                    {
                        title : $filter('translate')('F0102.calculater-title'),
                        defaultNum : dialog.portion_cutting_qty,
                        decimal : true
                    });
                }

                function pageNumberInfoGet(content, callback){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SYS.page_number_info_get',
                        content : content,
                        success : function(data){
                            if (callback)
                                callback(data.page_number_info_get);
                        }
                    });
                }

                function beforeOpenDialog(callback){
                    if(callback)
                        callback();
                }

                function openDialog(materialList){
                    $mdDialog.dialog(
                        'module/moduleF/program/F0104/F0104.html',
                        function (dialog) {
                            return {
                                css_url:'module/moduleF/program/F0104/F0104.css',
                                wo_no: options.wo_no,
                                op_no: options.op_no,
                                machine_no: options.machine_no,
                                material_detail: options.material_item,
                                portion_cutting_qty: options.material_item.qty, //部分下料數量
                                input_qty: function(event){
                                    var target = event.currentTarget;
                                    inputQty(target, dialog);
                                },
                                confirm: function(){
                                    console.log('material_detail',dialog.material_detail)
                                    beforePortionCutting(dialog,function(){
                                        dialog.hide();
                                        options.callback();
                                    });
                                },
                                back: function () {
                                    dialog.hide();
                                }
                            };
                        }
                    );
                }

                beforeOpenDialog(function(){
                    openDialog();
                });
            };
        }]);
});
